const app = getApp();
Page({
  data: {
    num: 0,
    scroll: 0,
    numList: [{
      name: '1天',
      select:false
    }, {
      name: '2天',
      select:false
    }, {
      name: '3天',
      select:false
    }, {
      name: '4天',
      select:false
    },{
      name: '5天',
      select:false
    },{
      name: '6天',
      select:false
    },{
      name: '7天',
      select:false
    }],
    is_sign:false,
    userInfo:[],
    button_msg:'我要签到',
    history_list:[],
  },

  onShow:function(){
    let _this = this;
      _this.getUserInfo();      
  },

  //获取个人信息
  getUserInfo:function(){
    let _this = this,
      numList = _this.data.numList;
    app._post('api/member/getUserInfo', {}, function(result) {
      if(result.code == 200){
        var continuity_day = result.data.continuity_day;
        for(var p in numList){
          if(p <= (continuity_day-1)){
            numList[p].select = true;
          }else{
            numList[p].select = false;
          }
        }
        _this.setData({
          numList:numList
        })
        _this.get_history_list();
        _this.check_qiandao();
      }else{
        wx.navigateTo({
          url: '../auth/auth'
        })
      }
    });
  },

  //获取签到记录
  get_history_list:function(){
    let _this = this;
    app._post('api/member/get_nearest_history',{}, function(result){
      _this.setData({
        history_list:result.data
      })
    }, 'json');
  },

  //检测今天是否有签到
  check_qiandao:function(){
    let _this = this;
    app._post('api/member/check_qiandao',{}, function(result){
      if(result.data != ''){
        _this.setData({
          is_sign:true,
          button_msg:'今天已签到'
        })
      }else{
        _this.setData({
          is_sign:false
        })
      }
    }, 'json');
  },

  //签到
  qiandao:function(){
    let _this = this,
      is_sign = _this.data.is_sign;
      if(is_sign){
        app.showError('您今天已经签到过');
        return false;
      }
      app._post('api/member/qiandao',{}, function(result){
        wx.showToast({
          title: result.msg,
          icon: 'none',
          duration: 1500
        })
        if(result.code == 200){
          _this.getUserInfo();
        }
      }, 'json');
  },
})